package com.example.springevent.service;

import com.example.springevent.event.UserRegisteredEvent;
import com.example.springevent.model.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {
    
    private final ApplicationEventPublisher eventPublisher;
    
    public User registerUser(User user) {
        // 模拟用户注册逻辑
        log.info("注册用户: {}", user.getUsername());
        user.setId(System.currentTimeMillis()); // 模拟ID生成
        
        // 发布用户注册事件
        eventPublisher.publishEvent(new UserRegisteredEvent(this, user));
        
        return user;
    }
}